<?php
namespace app\admin\model;

use app\common\model\JoinModel;
use think\Db;
use think\Model;

class Admin extends Model
{

    protected $name = 'admin';
    public  static function findInfoInfoByName($userName){
        if(ADMINTYPE ==2){
            $info =  JoinModel::where('username',$userName)
                ->find();
            if(!$info)return false;
            $info->lock = $info->is_lock;
            $info->is_supper = 1;
            return $info;
//                ->column("username","nickname","password","is_lock as lock","region_id","region_name");
//            "username,nickname,password,is_lock as lock,region_id,region_name"
        }else{
            return self::where('username',$userName)->find();
        }
    }
    public static function getRole($field='*'){
        $data = Db::name('role')->field($field)->select();
        return $data;
    }
    public static function getRoleName($role_id){
        return Db::name('role')->where('id',$role_id)->value('name');
    }
    public function setAdminByid($id,$name,$value){
        return $this->where('id',$id)->update([$name=>$value]);
    }
    public function getUserNameById($username){
        return $this->where('username',$username)->value('nickname');
    }
    public static function addRold($name,$is_active,$remarks){
        $array =[
            'name'=>$name,
            'is_active'=>$is_active,
            'remarks'=>$remarks
        ];
        return Db::name('role')->insert($array);
    }
    public static function getAdmin($field='*'){
        return self::field($field)->order('id','desc')->select();
    }
    public static function getAdminInfoByid($id,$field='*'){
        return self::field($field)->where('id',$id)->field($field)->find();
    }
    /**
     * 添加角色绑定数据
     */
    public static function addRoleAccess($arr,$role_id){
        Db::name('role_access')->where('role_id',$role_id)->delete();
        return Db::name('role_access')->insertAll($arr);
    }
    private  static function cation($arr,$num=0,$m=1,$router_id='id')
    {
        $list = [];
        foreach($arr as $k=>$v){
            if($v['parent_id'] === $num){
                $v['level'] = $m;
                $v['son'] = self::cation($arr,$v[$router_id],$m+1);
                $list[] = $v;
            }
        }
        return $list;
    }
    /**
     * @param $role_id
     * @param 菜单
     */
    public static function getMenuListByRole($role_id,$field='*'){
        $list = Db::name('role_access')
            ->where('role_id',$role_id)
            ->field($field)
            ->select();
        return self::cation($list,$num=0,$m=1,$router_id='router_id');
    }
    public  function addUser($array){
        $array['create_time'] = date("Y-m-d H:i:s");
        return $this->allowField(true)->save($array);
    }
    public function editUser($id,$array){
        return $this->allowField(true)->save($array,['id'=>$id]);
    }

}